import struct
import os
print(__file__)
file=open("model/gumi.pmd","rb")
def tell_pos(file,print_info:str):
    print(print_info,file.tell())
def parse(file):
    pmd={}
    pmd["header"]=read_header(file)
    pmd["model_name"]=read_model_name(file)
    pmd['model_comment']=read_model_comment(file)
    pmd['vertex']=read_vertex(file)
    pmd['index']=read_index(file)
    pmd["material"]=read_material(file)
    return pmd
def read_header(file):
    header_magic=file.read(3)
    if header_magic.decode()!="Pmd":
        print("不是pmd文件")
        return
    else:
        print("Loading pmd...")
        version_info=file.read(4)
        version_info=struct.unpack('<f',version_info)[0]
        return version_info
def read_model_name(file):
    model_name=file.read(20).split(b"\x00")[0].decode("shift-jis")
    return model_name
def read_model_comment(file):
    comment=file.read(256).split(b"\x00")[0]
    comment=comment.decode("shift-jis")
    return comment
#读取顶点
def read_vertex(file):
    chunk_size=int.from_bytes(file.read(1),byteorder='little', signed=False)
    print("顶点块大小",chunk_size)
    vertexs=[]
    for i in range(chunk_size):
        each_vertex={}
        #xyz位置
        x_pos=struct.unpack("f",file.read(4))[0]
        y_pos=struct.unpack("f",file.read(4))[0]
        z_pos=struct.unpack("f",file.read(4))[0]
        #法线
        x_normal=struct.unpack("f",file.read(4))[0]
        y_normal=struct.unpack("f",file.read(4))[0]
        z_normal=struct.unpack("f",file.read(4))[0]
        #纹理坐标uv
        u=struct.unpack("f",file.read(4))[0]
        v=struct.unpack("f",file.read(4))[0]
        #bone0 bone1 weight
        bone_0=struct.unpack("H",file.read(2))[0]
        bone_1=struct.unpack("H",file.read(2))[0]
        bone_0_weight=file.read(1)
        #edge flag
        edge_flag=struct.unpack("c",file.read(1))[0]
        bbb=edge_flag.decode("shift-jis")
        #print("edge循环边",bbb)
        each_vertex['x']=x_pos
        each_vertex['y']=y_pos
        each_vertex['z']=z_pos
        each_vertex['x_normal']=x_normal
        each_vertex['y_normal']=y_normal
        each_vertex['z_normal']=z_normal
        each_vertex['u']=u
        each_vertex['v']=v
        each_vertex['bone_0']=bone_0
        each_vertex['bone_1']=bone_1
        each_vertex['bone_0_weight']=bone_0_weight
        if edge_flag==b"\x00":
            each_vertex['edge_flag']=0
        vertexs.append(each_vertex)
    return vertexs 
#索引信息
def read_index(file):
    id=[]
    tell_pos(file,"索引信息开始")
    chunk_size=struct.unpack("L",file.read(4))[0]
    chunk_size=int(chunk_size/3)
    #file.seek(chunk_size-1,1)
    print("索引信息块大小",chunk_size)
    for i in range(chunk_size):
        vertex_id=file.read(2)
        if len(vertex_id)==2:
            print("顶点id",struct.unpack("H",vertex_id)[0])
            id.append(vertex_id)
    tell_pos(file,"索引信息结束")         
    return id 
#材质信息
def read_material(file):
    tell_pos(file,"读取到材质信息，当前位置")
    size=file.read(4)
    chunk_size=struct.unpack("L",size)
    #print("材质",chunk_size)
    mat=[]
    for i in range(chunk_size):
        #漫反射
        material={}
        R_diffuse=struct.unpack("f",file.read(4))[0]
        G_diffuse=struct.unpack("f",file.read(4))[0]
        B_diffuse=struct.unpack("f",file.read(4))[0]
        A_diffuse=struct.unpack("f",file.read(4))[0]
        material['r_diffuse']=R_diffuse
        material['g_diffuse']=G_diffuse
        material['b_diffuse']=B_diffuse
        material['a_diffuse']=A_diffuse
        print("材质",material)
        mat.append(material)
    return mat
#骨骼信息
def read_bone():
    bone_name=file.read(20)
    bone_parent_index=file.read(2)
    short_bone_parent_index=struct.unpack("h",bone_parent_index)[0]
    bone_type=file.read(1)
    target_bone=file.read(2)
    bone_x=file.read(4)
    bone_y=file.read(4)
    bone_z=file.read(4)
    char_bone_type=struct.unpack('c',bone_type)[0]
    #shift-jis
    print("骨骼名称",bone_name)
    print("骨骼parent",short_bone_parent_index)
    print("targetbone:",char_bone_type)
    #未写完
    file.close()
